package com.zjpavt.client.page;

import com.zjpavt.client.enums.ExtendDeviceEnum;
import com.zjpavt.client.extenddevice.voltagecurrent.ITemperatureAndHumidityDevice;
import com.zjpavt.client.hal.ExtendDeviceManager;
import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.page.abstractpage.AbstractBasePage;
import com.zjpavt.client.page.abstractpage.AbstractEditablePage;
import com.zjpavt.client.page.basepage.BaseSonPage;
import com.zjpavt.client.page.editableunit.EditUnitNum;
import com.zjpavt.client.page.intefacepage.ParentPage;
import com.zjpavt.client.page.intefacepage.SonPage;
import com.zjpavt.client.page.pageunit.ScreenLine;
import com.zjpavt.client.util.Consts;
import com.zjpavt.client.util.LoggerUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 恒温箱温湿度显示页面
 * @author zyc
 */
@Slf4j
public class TemperatureAndHumidityPage extends AbstractEditablePage implements SonPage {
    private static final String UNKNOWN_VALUE = "***";
    private static final DecimalFormat TEMPERATURE_FORMAT = new DecimalFormat("000.00");
    private SonPage sonPageImpl;

    private ParentPage parentPage;
    public TemperatureAndHumidityPage(int pageNum, ParentPage parentPage, long mRefreshPeriod) {
        super(pageNum, mRefreshPeriod);
        this.parentPage = parentPage;
        sonPageImpl = new BaseSonPage(getPageNum(), parentPage, 0, this);
    }

    @Override
    public void initEditUnits() {
        clearEditUnit();
//        有恒温箱的时候的可编辑块
        addEditUnit(new EditUnitNum(3, 10, 11, 0, 90, 1));
    }

    @Override
    public void setDefaultVal() {
        if (isDeviceExits()) {
            ITemperatureAndHumidityDevice sensor = (ITemperatureAndHumidityDevice) ExtendDeviceManager.getInstance().getExtendDevice(ExtendDeviceEnum.TMP);
            getEditUnit(0).setCurrentVal(sensor.getTemperatureThreshold());
        }
    }

    @Override
    public void saveVal() {
        try {
//			获取到页面的值 单位为1°C  而设置的值为0.1°C
            int temperatureThreshold = Integer.parseInt(getEditUnit(0).getText());
            if (isDeviceExits()) {
                ITemperatureAndHumidityDevice sensor = (ITemperatureAndHumidityDevice) ExtendDeviceManager.getInstance().getExtendDevice(ExtendDeviceEnum.TMP);
                sensor.setTemperatureThreshold(temperatureThreshold);
            }
        } catch (Exception e) {
            log.error(LoggerUtil.convertException2String(e));
        }
    }

    @Override
    public void refreshPage() {
//        如果包含温湿度恒温箱的时候
        if (isDeviceExits()) {
            ITemperatureAndHumidityDevice sensor = (ITemperatureAndHumidityDevice) ExtendDeviceManager.getInstance().getExtendDevice(ExtendDeviceEnum.TMP);
            double currTmp = sensor.updateTmpInformation().getTemperature();
            int tmpThresholdLow = sensor.getTemperatureThreshold();
            double humidity = sensor.getHumidity();
            LedScreen.getInstance().setText(getPageContent(currTmp, humidity, tmpThresholdLow));
        } else {
            List<ScreenLine> screenLineList = new ArrayList<>();
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(ExtendDeviceEnum.TMP.getDeviceName()).pageNum(mPageNum).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(Consts.NO_DEVICE).build());
            screenLineList.add(ScreenLine.BLANK);
            screenLineList.add(ScreenLine.BLANK);
            LedScreen.getInstance().setText(screenLineList);
        }
    }

    /**
     * 设备是否存在
     * @return 设备是否存在
     */
    private boolean isDeviceExits() {
        return ExtendDeviceManager.getInstance().containsExtendDevice(ExtendDeviceEnum.TMP);
    }

    @Override
    protected Runnable getRefreshTask() {
        //此处按块刷新，给每个可编辑块按照当前时间赋值，只有值变化时才刷新当前内容
        return this::refreshPage;
    }

    /**
     * 包含温度传感器的时候的页面
     *
     * @param currTmp 温度
     * @param humidity 湿度
     * @param tmpThresholdLow 阈值
     * @return  页面信息
     */
    public List<ScreenLine> getPageContent(double currTmp, double humidity, int tmpThresholdLow) {
        List<ScreenLine> result = new ArrayList<>();
        String tmp;
        String humidityStr;
        if (currTmp == Double.MIN_VALUE) {
            tmp = UNKNOWN_VALUE;
        } else {
            DecimalFormat df = new DecimalFormat("000.0");
            tmp = df.format(currTmp);
        }
        if (humidity == Double.MIN_VALUE) {
            humidityStr = UNKNOWN_VALUE;
        } else {
            DecimalFormat df = new DecimalFormat("000.00");
            humidityStr = df.format(humidity);
        }
        result.add(new ScreenLine.ScreenLineBuilder().text("temp:" + tmp).pageNum(getPageNum()).build());
        result.add(new ScreenLine.ScreenLineBuilder().text("humi:+" + humidityStr).build());
        result.add(new ScreenLine.ScreenLineBuilder()
                   .text("Fan-thre:" + String.valueOf(tmpThresholdLow))
                   .build());
        result.add(ScreenLine.BLANK);
        return result;
    }

    @Override
    public void onKeyEnt() {
        if (isInEditMode()) {
            super.onKeyEnt();
        } else {
            sonPageImpl.exitSonPage();
        }
    }

    @Override
    public void onKeySet() {
//      当有温湿度传感器的时候才可以编辑
        if (isDeviceExits()) {
            super.onKeySet();
        }
    }

    @Override
    public ParentPage getParentPage() {
        return this.sonPageImpl.getParentPage();
    }

    @Override
    public AbstractBasePage setParentPage(ParentPage parentPage) {
        return sonPageImpl.setParentPage(parentPage);
    }

    @Override
    public void enterSonPage() {
        sonPageImpl.enterSonPage();
    }

    @Override
    public void exitSonPage() {
        sonPageImpl.enterSonPage();
    }

    @Override
    public void close() {
        super.close();
    }
}
